﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Main.Master" AutoEventWireup="true"
    CodeBehind="Update.aspx.cs" Inherits="APMS.WebSite.Views.User.Update" %>

<%@ Import Namespace="APMS.WebSite.Common" %>    
<%@ Import Namespace="APMS.WebSite.Models.DataStore" %>
<%@ Import Namespace="APMS.WebSite.Models.ViewData.UserControls" %>

<asp:Content ID="ScriptContent" ContentPlaceHolderID="head" runat="server">
    <script type="text/javascript" language="javascript">
        $(document).ready(function(){
            // setup projects widget
           $('.widget ul.widgetContent li.add').hover(function(){ 
                $(this).addClass("hover"); 
           },function(){ 
                $(this).removeClass("hover"); 
           }).click(function(){
                addUserProject($(this).children('input[type=hidden]').val(), $('#tx_userName').val());
           });
           // setup main form
           $('#bt_save').click(function() {
                updateUserProfile($('#tx_userName').val());
                return false;
           });
        });
        
        function addUserProject(projectIdToAdd, userNameToAdd) {
            $.post("/User/AddUserProject", {
	            projectId: projectIdToAdd, 
	            userName: userNameToAdd 
            }, function(response) {
                // Complete request, there is some error
                if(response != 'success') {
                    alert(response);
                } else {
                    // Update list item
                    $('input[type=hidden][value=' + projectIdToAdd + ']').parent('li.add').removeClass('add').addClass('done').append(" <a class=\"remove\" href=\"javascript:removeUserProject(" + projectIdToAdd + ", '" + userNameToAdd + "');\">[Leave]</a>");
                    $.jGrowl('Add user to project successfully', {speed:  'slow'});
	            }
            });
        }
        
        function removeUserProject(projectIdToRemove, userNameToRemove) {
            $.post("/User/RemoveUserProject", {
	            projectId: projectIdToRemove, 
	            userName: userNameToRemove 
            }, function(response) {
                // Complete request, there is some error
                if(response != 'success') {
                    alert(response);
                } else {
                    // Update list item
                    var targetItem = $('input[type=hidden][value=' + projectIdToRemove + ']').parent('li.done');
                    //$('input[type=hidden][value=' + projectIdToRemove + ']').parent('li.done').removeClass('done').addClass('add').children('a.remove').remove();
                    $(targetItem).removeClass('done').addClass('add').children('a.remove').remove();
                    $(targetItem).hover(function(){ 
                        $(this).addClass("hover"); 
                    },function(){ 
                        $(this).removeClass("hover"); 
                    }).click(function(){
                        addUserProject($(this).children('input[type=hidden]').val(), $('#tx_userName').val());
                    });
                    $.jGrowl('Remove user from project successfully', {speed:  'slow'});
	            }
            });
        }
        
        function updateUserProfile(userName) {
            $.post("/User/Update/" + userName, {
                id: userName, 
                password: $('#tx_password').val(), 
                email: $('#tx_email').val(), 
                firstName: $('#tx_firstName').val(), 
                lastName: $('#tx_lastName').val()
            }, function(response) {
                $.jGrowl('Update user successfully', {speed:  'slow'});
            });
        }
        
	</script>
</asp:Content>
<asp:Content ID="PageTitleContent" ContentPlaceHolderID="PageTitleHolder" runat="server">
    <h2 class="nodesc">Personal Information</h2>
    <p class="desc">Your information will not be distributed nor revealed to the others without your permission</p>
</asp:Content>
<asp:Content ID="NavigationMenuContent" ContentPlaceHolderID="NavigationMenuHolder" runat="server">
</asp:Content>
<asp:Content ID="CommandLinkContent" ContentPlaceHolderID="CommandLinkHolder" runat="server">
</asp:Content>
<asp:Content ID="SidebarContent" ContentPlaceHolderID="SidebarHolder" runat="server">
    <div class="widget">
        <h3 class="widgetTitle">Actions</h3>
        <div class="widgetContent">
            <%=Html.Image(AppHelper.GetThemePath() + "images/add.png", new { alt="New", title="New Project", style="vertical-align:top" }) %>
            <%=Html.ActionLink<ProjectController>(x=>x.Create(), "Create a new project") %>
        </div>
    </div>
    <div class="widget">
	    <h3 class="widgetTitle">Involved Projects</h3>
	    <ul class="widgetContent">
	        <% foreach (Project project in ViewData.Model.AllProjects as List<Project>)
            { %>
	        
	        <%
// Check whether current project was selected by user or not,
string projectItemString = String.Empty;
ProjectUser projUser = ViewData.Model.SelectedProjects.Find(delegate(ProjectUser pUser) { return pUser.ProjectID == project.ProjectID; });
if (projUser != null)
{
    // Found
    projectItemString = String.Format("<li class=\"done\">{0} <a class=\"remove\" href=\"javascript:removeUserProject({1}, '{2}');\">[Leave]</a><input type=\"hidden\" id=\"{1}\" value=\"{1}\"/></li>",
        project.ProjectName, 
        project.ProjectID,
        projUser.UserName);
}
else
{
    // Not involve yet 
    projectItemString = String.Format("<li class=\"add\">{0}<input type=\"hidden\" id=\"{1}\" value=\"{1}\"/></li>",
        project.ProjectName,
        project.ProjectID);
}
Response.Write(projectItemString);
            %>
	           
	        <% } %>
	    </ul>
    </div>
</asp:Content>
<asp:Content ID="MainContent" ContentPlaceHolderID="ContentHolder" runat="server">
    
    <form method="post" action="/User/Update/<%=ViewData.Model.CurrentUser.UserName %>">
    <%--<form method="post" action="<%=Url.Action(new{controller="User", action="Update", id=ViewData.Model.CurrentUser.UserName})%>">--%>
    <div id="mainForm">
        <h3>Required Information</h3>
        <p class="blockintro">Username and password for this account. Username cannot be changed but you can change the password.</p>
        <div class="block">
            <dl>
                <dt><label for="tx_userName">User Name:</label></dt>
                <dd><%=Html.TextBox("userName", new { id = "tx_userName", title = "User Name", size = "25", disabled = "disabled", value = ViewData.Model.CurrentUser.UserName })%></dd>
                <dt class="required"><label for="tx_password">Password:</label></dt>
                <dd><%=Html.TextBox("password", new { id = "tx_password", title = "Password", size = "25", maxlegnth = "30", value = ViewData.Model.CurrentUser.PassWord })%></dd>
                <dt class="required"><label for="tx_email">E-Mail:</label></dt>
                <dd><%=Html.TextBox("email", new { id = "tx_email", title = "E-Mail", size = "25", maxlegnth = "100", value = ViewData.Model.CurrentUser.Email })%></dd>
            </dl>
        </div>
        
        <%--Basic Information--%>
        <h3>Your Details</h3>
        <p class="blockintro">This person's name will appear with their messages, comments, and files and whenever they are responsible for a to-do</p>
        <div class="block">
            <dl>
                <dt class="required"><label for="tx_firstName">First Name</label></dt>
                <dd><%=Html.TextBox("firstName", new { id = "tx_firstName", title = "First Name", size = "25", maxlegnth = "50", value = ViewData.Model.CurrentUser.FirstName })%></dd>
                <dt><label for="tx_lastName">Last Name</label></dt>
                <dd><%=Html.TextBox("lastName", new { id = "tx_lastName", title = "Last Name", size = "25", maxlegnth = "50", value = ViewData.Model.CurrentUser.LastName })%></dd>
            </dl>
        </div>
       
        
        <%-- TODO: Image--%>
        
        <%-- Submit Action--%>
        <div class="action">
            <%-- TODO: Validate Form --%>
            <input id="bt_save" type="submit" class="button" value="Save Changes" />
         </div>
    </div>  
    </form>  
</asp:Content>
